package site.yunnong.atvris.portal.spider.haokan;

import com.alibaba.fastjson.JSONObject;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zjh
 * @date 2021/8/31 11:17
 */
@Slf4j
@Service("haokanPagePipeline")
//@PipelineName("haokanPagePipeline")
public class HaokanPagePipeline implements Pipeline<HaokanPage> {
    
    @Override
    public void process(HaokanPage bean) {
        log.info("HaoKanPagePipeline.process query:{}, pn:{}", bean.getQuery(), bean.getPn());
        List<JSONObject> list = bean.getRes().getList();
        for (JSONObject jsonObject : list) {
            String url = jsonObject.getObject(HaokanRes.URL, String.class);
            // 派生
            HttpGetRequest request = new HttpGetRequest(url);
            request.addParameter("duration", jsonObject.getObject(HaokanRes.DURATION, String.class));
            request.addParameter("cover", jsonObject.getObject(HaokanRes.COVER_SRC, String.class));
            request.addParameter("title", jsonObject.getObject(HaokanRes.TITLE, String.class));
            request.addParameter("pid", bean.getPid());
            request.addParameter("partitionName", bean.getQuery());
            request.addParameter("source", "好看视频");
            DeriveSchedulerContext.into(request);
        }
        log.info("HaoKanPagePipeline.process query:{}, pn:{} SUCCESS", bean.getQuery(), bean.getPn());
    }
}
